﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using weclever.Navigation;
using System.Web;
using System.Net;

using HtmlAgilityPack;
using System.Diagnostics;
using weclever.Infrastructure;

namespace weclever.Pages
{
    public class MainPage //:Page 
    {
        private string siteHtml = "";
        private HtmlNode node = null;
        private HtmlDocument doc = null;

        private IPageCommunication PageCommunication { get; set; }
        

        public MainPage(IPageCommunication pageCommunication)
        {
            PageCommunication = pageCommunication;
            siteHtml = PageCommunication.NavigateToURL(SiteUrls.Site);
            doc = new HtmlDocument();
            doc.LoadHtml(siteHtml);
            node = doc.DocumentNode;            
        }

        public List<City> Cities()
        {
            List<City> cities = new List<City>();
            HtmlNode cityNode = doc.GetElementbyId("weclever_city_select");
            //HtmlNodeCollection nodes = cityNode.SelectNodes("weclever_inner/weclever_action_opleft/weclever_action_opright");
            HtmlNodeCollection nodes = cityNode.SelectNodes("//div[@class='weclever_inner']/div[@class='weclever_action_opleft']/div[@class='weclever_action_opright']/ul/li/a");
            foreach (var n in nodes)
            {
                if (n.Name == "a")
                {                   
                    HtmlAttribute attrHref = n.Attributes["href"];
                    if (attrHref == null) continue;
                    List<string> lst = SiteUrls.UrlParse(attrHref.Value);                    
                    if (lst.Count != 1)
                    {
                        continue;
                    }                    
                    cities.Add(new City() { NameRu = n.InnerText, NameEng = lst[0] });
                }
            }
           // Console.WriteLine(nodes.Count);
            //Console.Write(cityNode.ChildNodes.Count); 
            return cities;
        }        
    }
}
